package com.xsh.opensltest;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.File;

// 临时作为通道机架测试
public class DawActivity extends AppCompatActivity
{
    public LinearLayout ChannelRack;
    public static String tag = DawActivity.class.getSimpleName();

    static {
        System.loadLibrary("slesengine");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //SlEngine.initEngine();
        setContentView(R.layout.activity_channel_rack);
        findViewById(R.id.playButton).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                SlEngine.play();
            }
        });
        findViewById(R.id.stopButton).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                SlEngine.stop();
            }
        });
        findViewById(R.id.gotoMixer).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(DawActivity.this, MixerActivity.class);
                startActivity(intent);
            }
        });
        ChannelRack = findViewById(R.id.ChannelRack);
        findViewById(R.id.openProject).setOnClickListener(new View.OnClickListener() {
                                                              @Override
                                                              public void onClick(View view)
                                                              {
                                                                  /*/Intent i = new Intent(DawActivity.this,FilePicker.class);
                                                                  startActivity(i);*/
                                                              }
                                                          });
        findViewById(R.id.saveProject).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                //startActivityForResult();
            }
        });
        findViewById(R.id.ExportFile).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(DawActivity.this);
                builder.setTitle("导出音频");
                builder.setView(R.layout.dialog_export);
                builder.create();
                builder.setPositiveButton("渲染", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i)
                    {

                    }
                });
                builder.show();
            }
        });
        //SlEngine.renderToFile();
                findViewById(R.id.settingButton).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(DawActivity.this);
                        builder.setTitle("设置");
                        builder.setView(R.layout.settings);
                        builder.create();
                        builder.show();
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                    }
                });
        findViewById(R.id.channelRackAdd).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(DawActivity.this);
                builder.setTitle("请选择乐器");
                LinearLayout container = new LinearLayout(DawActivity.this);
                container.setOrientation(LinearLayout.VERTICAL);
                container.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                ));

                File[] pluginDirs = App.appFilesDir.listFiles();

                if(pluginDirs !=null && pluginDirs.length>0)
                {
                    for(File f:pluginDirs)
                    {
                        Button instrument = new Button(DawActivity.this);
                        instrument.setText(f.getName());
                        instrument.setOnClickListener(onInstrumentAddListener);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                        container.addView(instrument,params);
                    }
                    builder.setView(container);
                }else{Toast.makeText(DawActivity.this,"你还没有乐器，请先在在插件管理导入乐器",Toast.LENGTH_LONG).show();}
                //Dialog dialog = builder.create();
                builder.create();
                builder.show();
            }
        });
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认退出?数据不会保存!");
        builder.setPositiveButton("退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            {
                finish();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();
    }

    // 添加乐器到通道机架界面
    public void addInstrumentUI(String name)
    {
        Button instrumentButton = new Button(DawActivity.this);
        instrumentButton.setText(name);
        //b.setOnClickListener();
        ChannelRack.addView(instrumentButton);
        // 显示乐器界面
        instrumentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                PluginView dialog = new PluginView(new File(App.appFilesDir,name+"/layout/ui.json").getAbsolutePath(),name);
                dialog.show(getSupportFragmentManager(), "");
                String so = name + File.separator + "lib" + File.separator + "lib" + name + ".so";
                //SlEngine.loadPlugin(new File(App.appFilesDir,so).getAbsolutePath());
                //SlEngine.setActiveInstrument();
            }
        });
        instrumentButton.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick(View view)
            {
                LinearLayout container = new LinearLayout(DawActivity.this);
                container.setOrientation(LinearLayout.VERTICAL);
                container.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                ));
                AlertDialog.Builder builder = new AlertDialog.Builder(DawActivity.this);
                //final Dialog instrumentMenu = null;
                //builder.setCancelable(false);
                builder.setTitle("乐器菜单");
                Button delButton = new Button(DawActivity.this);
                delButton.setText("删除");
                Button renameButton = new Button(DawActivity.this);
                renameButton.setText("重命名");
                Button pianoButton = new Button(DawActivity.this);
                pianoButton.setText("钢琴窗帘");
                renameButton.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        LinearLayout container = new LinearLayout(DawActivity.this);
                        container.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
                        EditText text = new EditText(DawActivity.this);
                        text.setHint("请输入名称");
                        AlertDialog.Builder builder1 = new AlertDialog.Builder(DawActivity.this);
                        builder1.setTitle("重命名");

                        text.setLayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                        container.addView(text);
                        builder1.setView(container);
                        builder1.create();
                        builder1.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i)
                                    {
                                        String name = text.getText().toString();
                                        instrumentButton.setText(name);
                                    }
                                });
                        builder1.show();
                    }
                });
                pianoButton.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        //new PianoRoll(DawActivity.this);
                        Toast.makeText(DawActivity.this,R.string.dev,Toast.LENGTH_SHORT).show();
                    }
                });
                delButton.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        ChannelRack.removeView(instrumentButton);
                        builder.create().dismiss();
                    }
                });
                container.addView(renameButton);
                container.addView(delButton);
                container.addView(pianoButton);
                builder.setView(container);
                builder.show();
                return false;
            }
        });
    }
    View.OnClickListener onInstrumentAddListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            String name = ((Button)view).getText().toString();
            addInstrumentUI(name);
        }
    };
}